package com.xuetang9.BBS.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.xuetang9.BBS.dao.TopicDao;
import com.xuetang9.BBS.domain.Topic;

/**
 * 帖子实现类
 * 
 * @author Administrator
 *
 */

@Repository
public class TopicDaoImpl implements TopicDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	private RowMapper<Topic> getTopicRowMapper() {
		return new RowMapper<Topic>() {

			@Override
			public Topic mapRow(ResultSet resultSet, int arg1) throws SQLException {
				Topic Topic = new Topic();
				Topic.setAccount(resultSet.getString("account"));
				Topic.setContent(resultSet.getString("content"));
				Topic.setLastRid(resultSet.getString("lastRid"));
				Topic.setPubdate(resultSet.getDate("pubdate"));
				Topic.setReplyCount(resultSet.getString("replyCount"));
				Topic.setReplyVisible(resultSet.getString("replyVisible"));
				Topic.setSid(resultSet.getString("sid"));
				Topic.setTid(resultSet.getString("tid"));
				Topic.setTitle(resultSet.getString("title"));

				return Topic;
			}

		};
	}

	/**
	 * 查询帖子子信息
	 */
	@Override
	public List<Topic> selectTopic(Topic top) {

		String sql = "select * from section ";
		RowMapper<Topic> rm = getTopicRowMapper();
		return jdbcTemplate.query(sql, rm);

	}

	/**
	 * 新建帖子
	 */
	@Override
	public int save(Topic topic) {
		String sql = "insert into topic values( default,?,?,?,?,?,?,?,?,?,?,?,?,?)";

		return jdbcTemplate.update
				(sql, topic.getSid(),
					  topic.getAccount(), 
						topic.getTtid(), 
						topic.getTitle(),
					    topic.getContent(), 
						topic.getPubdate(), 
						topic.getReplyCount(),
						topic.getVisitCount(),
						topic.getReplyVisible(), 
						topic.getVisibleLength(), 
						topic.getLastRid(), 
						topic.getEssence(),
						topic.getOnTop());
	
		}

}
